package com.lzs.bootsafety.business.oss.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzs.bootsafety.business.oss.entity.SysFile;
import com.lzs.bootsafety.business.oss.mapper.SysFileMapper;
import com.lzs.bootsafety.business.oss.service.SysConfigService;
import com.lzs.bootsafety.business.oss.service.SysFileService;
import com.lzs.bootsafety.business.sys.entity.SysUser;
import com.lzs.bootsafety.common.oss.Exception.OssException;
import com.lzs.bootsafety.common.oss.FileUpload;
import com.lzs.bootsafety.common.oss.GlobalFileUpload;
import com.lzs.bootsafety.common.util.enums.SysConfig;
import com.lzs.bootsafety.common.util.oss.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 *
 *  @author  zhanshi Liu
 *  @date    2020/04/20
 *  @website www.jhmsona.cn
 *
 */
@Service
public class SysFileServiceImpl implements SysFileService {
    @Autowired
    private SysFileMapper sysFileMapper;
    @Autowired
    private SysConfigService sysConfigService;
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertSysFile(SysFile sysFile) {
        sysFile.setCreateTime(new Date());
        sysFile.setUpdateTime(new Date());
        return sysFileMapper.insertSelective(sysFile);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int upload(MultipartFile[] file){
        if (null == file || file.length == 0) {
            throw new OssException("请至少选择一个文件！");
        }
        for (MultipartFile multipartFile : file) {
            //通过魔数值获取文件类型  性能不好  对安全没有要求的用后缀验证
            //getFileType(multipartFile.getInputStream());
            FileUpload uploader = new GlobalFileUpload();
            uploader.upload(multipartFile,true);
        }
        return file.length;
    }

    @Override
    public SysFile getByPrimaryKey(Long id) {
        return sysFileMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void download(Long id){
        SysFile sysFile = this.getByPrimaryKey(id);
        FileUpload upload = new GlobalFileUpload();
        upload.download(sysFile.getFilePath(),sysFile.getStoreType());
    }

    @Override
    public SysFile getByPathAndStorageType(String filePath, String storageType) {
        return sysFileMapper.selectByPathAndStorageType(filePath,storageType);
    }

    @Override
    public PageInfo<SysFile> selectSysFileByPage(SysFile sysFile) {
        Integer page = sysFile.getPage();
        Integer limit = sysFile.getLimit();
        PageHelper.startPage(page = page == null ? 1 : page,limit = limit == null ? 10 : limit);
        List<SysFile> sysFileList = sysFileMapper.getSysFileByPage(sysFile);
        return new PageInfo<SysFile>(sysFileList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int delByPrimaryKey(Long id) {
        SysFile sysFile = this.getByPrimaryKey(id);
        FileUpload upload = new GlobalFileUpload();
        upload.delete(sysFile.getFilePath(),sysFile.getStoreType());
        return sysFileMapper.deleteByPrimaryKey(id);
    }

    public void getFileType(InputStream inputStream){
        try {
            String fileType = String.valueOf(FileUtils.getType(inputStream));
            if (StringUtils.isEmpty(fileType)){
                Map<String ,Object> sysConfigs = sysConfigService.getConfigs();
                String fileSuffix = String.valueOf(sysConfigs.get(SysConfig.FILE_SUFFIX.getKey()));
                throw new OssException("非法的文件！目前只支持以下文件格式:"+"["+fileSuffix.toLowerCase()+"]");
            }
        } catch (IOException e) {
            throw new OssException("获取文件魔数值异常"+e.getMessage());
        }

    }
}
